import nuke

sn=nuke.selectedNode()
x=sn.xpos()
y=sn.ypos()

cs=sn.channels()
layers=sorted( list(set( i.split('.')[0] for i in cs)))
print layers

master_layer_prex=['diffuse','specular','sss','occ']

occ_list=[]
diffuse_list=[]

other_list=[]
for c in layers:
    if c.lower().count('diffuse')>0:
        diffuse_list.append(c)
    elif c.lower().count('specular')>0:
        diffuse_list.append(c) #也算diffuse里面
    elif c.lower().count('occ')>0:
        occ_list.append(c)
    elif c.lower().count('sss')>0:
        diffuse_list.append(c)  #sss也算diffuse里面
    else:
        other_list.append(c)

o_merge=None
d_merge=None
delta_x=0
if occ_list:
    occ_grades=[]
    for o in occ_list:
        sf=nuke.createNode('Shuffle')
        sf.setName('Shuffle_'+o)
        sf['postage_stamp'].setValue(1)
        sf.setInput(0,sn)
        sf['in'].setValue(o)
        sf.setXpos(x+delta_x)
        sf.setYpos(y+150)
        grade=nuke.createNode('Grade')
        grade.setName('Grade_'+o)
        grade.setInput(0,sf)
        grade.setXpos(x+delta_x)
        grade.setYpos(y+300)
        delta_x+=150

    o_merge=nuke.createNode('Merge')
    o_merge['operation'].setValue('mult')
    o_merge.setXpos(x+delta_x-150)
    o_merge.setYpos(y+600)
    for i in range(len(occ_grades)):
        o_merge.setInput(i,occ_grades[i])


if diffuse_list:
    # delta_x+=150 #先偏移一波
    d_grades=[]  #使用grade来缓解一波

    for d in diffuse_list:
        sf=nuke.createNode('Shuffle')
        sf.setName('Shuffle_' + d)
        sf['postage_stamp'].setValue(1)
        sf.setInput(0, sn)
        sf['in'].setValue(d)
        sf.setXpos(x+delta_x)
        sf.setYpos(y+150)
        grade=nuke.createNode('Grade')
        grade.setName('Grade_' + d)
        grade.setXpos(x+delta_x)
        grade.setYpos(y+300)
        grade.setInput(0,sf)
        if d.lower() in ['diffuse','sss'] :
            d_grades.append(grade)
        delta_x+=150

    d_merge=nuke.createNode('Merge')
    d_merge['operation'].setValue('plus')
    d_merge.setXpos(x+150)
    d_merge.setYpos(y + 450)
    for g in range(len(d_grades)):
        d_merge.setInput(g,d_grades[g])











